package com.zretc.controller;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.zretc.entity.Collect;
import com.zretc.entity.Song;
import com.zretc.service.CollectService;
import com.zretc.service.SongService;
import com.zretc.service.impl.CollectServiceImpl;
import com.zretc.service.impl.SongServiceImpl;
import com.zretc.util.PageInfo;

/**
 * Servlet implementation class CollectServlet
 */
@WebServlet("/CollectServlet")
@MultipartConfig
public class CollectServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private CollectService collectService = new CollectServiceImpl();
    private SongService songService = new SongServiceImpl();
    Gson gson = new Gson();
    
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CollectServlet() {
        super();
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String op = request.getParameter("op");
		if ("addMulitCollect".equals(op)) {
			doMulitAddCollect(request,response);
		}else if ("deleteCollect".equals(op)) {
			doDeleteCollect(request,response);
		}else if ("getCollectList".equals(op)) {
			doGetCollectList(request,response);
		}else if ("getMyCollectionSong".equals(op)) {
			doGetMyCollectionSong(request,response);
		}
	}
	
	//获取该用户的收藏歌曲列表
	private void doGetMyCollectionSong(HttpServletRequest request, HttpServletResponse response) throws IOException {
		Map<String, String> map = new HashMap<String, String>();
		String accountId = request.getParameter("accountId");
		String pageNum = request.getParameter("pageNum");
		String pageSize = request.getParameter("pageSize");
		map.put("accountId", accountId);
		map.put("pageNum", pageNum);
		map.put("pageSize", pageSize);
		PageInfo<Collect> collectByAccountId = collectService.getCollectByAccountId(map);
		String json = gson.toJson(collectByAccountId);
		response.getWriter().write(json);
	
	}

	//判断用户播放列表的歌曲是否收藏
	private void doGetCollectList(HttpServletRequest request, HttpServletResponse response) throws IOException {
		Map<String, Object> map = new HashMap<String, Object>();
		String songName = request.getParameter("songName");
		String accountId = request.getParameter("accountId");
		List<Song> songBySongName = songService.getSongBySongName(songName);
		
		map.put("songId", songBySongName.get(0).getSongId());
		Collect collect = collectService.getCollect(songBySongName.get(0).getSongId(), Integer.parseInt(accountId));
		if (collect==null) {
			response.getWriter().write("no");
		}else {
			response.getWriter().write("yes");
		}
		
	}
	//删除收藏歌曲
	private void doDeleteCollect(HttpServletRequest request, HttpServletResponse response) throws IOException {
		Map<String, Object> map = new HashMap<String, Object>();
		Integer songId = Integer.parseInt(request.getParameter("songId"));
		String accountId = request.getParameter("accountId");
		map.put("accountId", accountId);
		map.put("songId", songId);
		int add = collectService.unCollect(map);
		if (add>0) {
			response.getWriter().write("yes");
		}else {
			response.getWriter().write("no");
		}
	}
	//批量收藏歌曲(上方收藏按钮)
	private void doMulitAddCollect(HttpServletRequest request, HttpServletResponse response) throws IOException {
		Map<String, Object> map = new HashMap<String, Object>();
		String accountId = request.getParameter("accountId");
		map.put("accountId", accountId);
		String song = request.getParameter("song");
		Type type = new TypeToken<ArrayList<Song>>(){}.getType();
		ArrayList<Song> songList = gson.fromJson(song, type);
		for (int i = 0; i < songList.size(); i++) {
			Integer songId = songList.get(i).getSongId();
			Collect collect = collectService.getCollect(songId, Integer.parseInt(accountId));
			if (collect!=null) {
				response.getWriter().write("no");
				break;
			}else {
				map.put("songId", songId);
				collectService.addCollect(map);
			}
		}
	}

}
